package com.exp.bookhouse.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exp.bookhouse.mapper.CategoryMapper;
import com.exp.bookhouse.pojo.Category;
import com.exp.bookhouse.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 保存分类信息
     * @param category
     * @return
     */
    @Override
    public int saveCategory(Category category) {
        return categoryMapper.insert(category);
    }

    /**
     * 查询所有分类
     * @return
     */
    @Override
    public List<Category> queryAllCategory() {
        return categoryMapper.selectList(null);
    }

    /**
     * 分页查询分类
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Category> queryCategoryByPage(Integer page, Integer size) {
        IPage<Category> iPage = new Page<>(page,size);
        return categoryMapper.selectPage(iPage,null);
    }

    /**
     * 根据id查询分类信息
     * @param id
     * @return
     */
    @Override
    public Category queryCategoryById(Long id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateById(category);
    }


    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        //删除该分类的所有书籍
        categoryMapper.deleteBookByCategoryId(id);
        //删除分类
        categoryMapper.deleteById(id);
    }


}
